package com.zhm.demo.util;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hthium.http.client.HcHttpClient;
import com.hthium.http.client.HcHttpClientHelper;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class WeatherUtil {

    // # 根据经纬度获取天气信息
    private static final String getWeatherUrl = "https://atlas.microsoft.com/weather/currentConditions/json";
    //   # 根据经纬度获取地址信息
    private static final String getAddressUrl = "https://atlas.microsoft.com/search/address/reverse/json";
    private static final String xMsClientId = "4d0b8acc-e18d-46d1-93ed-f210e6a93862";
    private static final String subscriptionKey = "jdn7-DYh2V1xjTCcnox0HQR_aQNMxNpAgoAx2OVdz1E";

    private static final String unit = "metric";

    //"应返回搜索结果的语言。 默认 zh-HanS-CN .请参考 <a href=\"https://learn.microsoft.com/zh-cn/azure/azure-maps/supported-languages\">查看支持的语言</a>"

    public static void getWeather(){
        String lat = "22.5445741";
        String lng = "114.0545429";
        String language = "zh-HanS-CN";
        String param = "api-version=" + "1.1" +
                "&subscription-key=" + subscriptionKey +
                "&query=" + lat + "," + lng +
                "&language=" + language +
                "&details=" + true +
                "&duration=" + "0";
        Map<String, Object> header = new HashMap<>();
        header.put("x-ms-client-id", xMsClientId);
        // 请求
        String s = null;
        try {
            s = HcHttpClientHelper.defaultClient().get(getWeatherUrl+"?"+param).header(header).exec().strBody();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        System.out.println(s);
    }

    public static void main(String[] args) {
        getWeather();
    }


    @NoArgsConstructor
    @Data
    public static class MicroWeatherRes{

        /*
        {"results":[{"dateTime":"2024-04-23T19:33:00+08:00","phrase":"多云","iconCode":7,"hasPrecipitation":false,"isDayTime":false,"temperature":{"value":24.3,"unit":"C","unitType":17},"realFeelTemperature":{"value":26.6,"unit":"C","unitType":17},"realFeelTemperatureShade":{"value":26.6,"unit":"C","unitType":17},"relativeHumidity":88,"dewPoint":{"value":22.3,"unit":"C","unitType":17},"wind":{"direction":{"degrees":135.0,"localizedDescription":"东南"},"speed":{"value":12.6,"unit":"km/h","unitType":7}},"windGust":{"speed":{"value":16.2,"unit":"km/h","unitType":7}},"uvIndex":0,"uvIndexPhrase":"低","visibility":{"value":16.1,"unit":"km","unitType":6},"obstructionsToVisibility":"","cloudCover":99,"ceiling":{"value":9144.0,"unit":"m","unitType":5},"pressure":{"value":1008.0,"unit":"mb","unitType":14},"pressureTendency":{"localizedDescription":"上升","code":"R"},"past24HourTemperatureDeparture":{"value":-0.8,"unit":"C","unitType":17},"apparentTemperature":{"value":27.2,"unit":"C","unitType":17},"windChillTemperature":{"value":24.4,"unit":"C","unitType":17},"wetBulbTemperature":{"value":22.9,"unit":"C","unitType":17},"precipitationSummary":{"pastHour":{"value":0.0,"unit":"mm","unitType":3},"past3Hours":{"value":0.0,"unit":"mm","unitType":3},"past6Hours":{"value":0.0,"unit":"mm","unitType":3},"past9Hours":{"value":6.3,"unit":"mm","unitType":3},"past12Hours":{"value":25.3,"unit":"mm","unitType":3},"past18Hours":{"value":25.3,"unit":"mm","unitType":3},"past24Hours":{"value":39.5,"unit":"mm","unitType":3}},"temperatureSummary":{"past6Hours":{"minimum":{"value":24.3,"unit":"C","unitType":17},"maximum":{"value":26.5,"unit":"C","unitType":17}},"past12Hours":{"minimum":{"value":24.3,"unit":"C","unitType":17},"maximum":{"value":27.0,"unit":"C","unitType":17}},"past24Hours":{"minimum":{"value":23.0,"unit":"C","unitType":17},"maximum":{"value":27.0,"unit":"C","unitType":17}}}}]}
         */

        @JsonProperty("results")
        private List<ResultsDTO> results;

        @NoArgsConstructor
        @Data
        public static class ResultsDTO {
            @JsonProperty("dateTime")
            private String dateTime;
            @JsonProperty("phrase")
            private String phrase;
            @JsonProperty("iconCode")
            private Integer iconCode;
            @JsonProperty("hasPrecipitation")
            private Boolean hasPrecipitation;
            @JsonProperty("isDayTime")
            private Boolean isDayTime;
            @JsonProperty("temperature")
            private TemperatureDTO temperature;
            @JsonProperty("realFeelTemperature")
            private RealFeelTemperatureDTO realFeelTemperature;
            @JsonProperty("realFeelTemperatureShade")
            private RealFeelTemperatureShadeDTO realFeelTemperatureShade;
            @JsonProperty("relativeHumidity")
            private Integer relativeHumidity;
            @JsonProperty("dewPoint")
            private DewPointDTO dewPoint;
            @JsonProperty("wind")
            private WindDTO wind;
            @JsonProperty("windGust")
            private WindGustDTO windGust;
            @JsonProperty("uvIndex")
            private Integer uvIndex;
            @JsonProperty("uvIndexPhrase")
            private String uvIndexPhrase;
            @JsonProperty("visibility")
            private VisibilityDTO visibility;
            @JsonProperty("obstructionsToVisibility")
            private String obstructionsToVisibility;
            @JsonProperty("cloudCover")
            private Integer cloudCover;
            @JsonProperty("ceiling")
            private CeilingDTO ceiling;
            @JsonProperty("pressure")
            private PressureDTO pressure;
            @JsonProperty("pressureTendency")
            private PressureTendencyDTO pressureTendency;
            @JsonProperty("past24HourTemperatureDeparture")
            private Past24HourTemperatureDepartureDTO past24HourTemperatureDeparture;
            @JsonProperty("apparentTemperature")
            private ApparentTemperatureDTO apparentTemperature;
            @JsonProperty("windChillTemperature")
            private WindChillTemperatureDTO windChillTemperature;
            @JsonProperty("wetBulbTemperature")
            private WetBulbTemperatureDTO wetBulbTemperature;
            @JsonProperty("precipitationSummary")
            private PrecipitationSummaryDTO precipitationSummary;
            @JsonProperty("temperatureSummary")
            private TemperatureSummaryDTO temperatureSummary;

            @NoArgsConstructor
            @Data
            public static class TemperatureDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class RealFeelTemperatureDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class RealFeelTemperatureShadeDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class DewPointDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class WindDTO {
                @JsonProperty("direction")
                private DirectionDTO direction;
                @JsonProperty("speed")
                private SpeedDTO speed;

                @NoArgsConstructor
                @Data
                public static class DirectionDTO {
                    @JsonProperty("degrees")
                    private Double degrees;
                    @JsonProperty("localizedDescription")
                    private String localizedDescription;
                }

                @NoArgsConstructor
                @Data
                public static class SpeedDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }
            }

            @NoArgsConstructor
            @Data
            public static class WindGustDTO {
                @JsonProperty("speed")
                private SpeedDTO speed;

                @NoArgsConstructor
                @Data
                public static class SpeedDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }
            }

            @NoArgsConstructor
            @Data
            public static class VisibilityDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class CeilingDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class PressureDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class PressureTendencyDTO {
                @JsonProperty("localizedDescription")
                private String localizedDescription;
                @JsonProperty("code")
                private String code;
            }

            @NoArgsConstructor
            @Data
            public static class Past24HourTemperatureDepartureDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class ApparentTemperatureDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class WindChillTemperatureDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class WetBulbTemperatureDTO {
                @JsonProperty("value")
                private Double value;
                @JsonProperty("unit")
                private String unit;
                @JsonProperty("unitType")
                private Integer unitType;
            }

            @NoArgsConstructor
            @Data
            public static class PrecipitationSummaryDTO {
                @JsonProperty("pastHour")
                private PastHourDTO pastHour;
                @JsonProperty("past3Hours")
                private Past3HoursDTO past3Hours;
                @JsonProperty("past6Hours")
                private Past6HoursDTO past6Hours;
                @JsonProperty("past9Hours")
                private Past9HoursDTO past9Hours;
                @JsonProperty("past12Hours")
                private Past12HoursDTO past12Hours;
                @JsonProperty("past18Hours")
                private Past18HoursDTO past18Hours;
                @JsonProperty("past24Hours")
                private Past24HoursDTO past24Hours;

                @NoArgsConstructor
                @Data
                public static class PastHourDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }

                @NoArgsConstructor
                @Data
                public static class Past3HoursDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }

                @NoArgsConstructor
                @Data
                public static class Past6HoursDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }

                @NoArgsConstructor
                @Data
                public static class Past9HoursDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }

                @NoArgsConstructor
                @Data
                public static class Past12HoursDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }

                @NoArgsConstructor
                @Data
                public static class Past18HoursDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }

                @NoArgsConstructor
                @Data
                public static class Past24HoursDTO {
                    @JsonProperty("value")
                    private Double value;
                    @JsonProperty("unit")
                    private String unit;
                    @JsonProperty("unitType")
                    private Integer unitType;
                }
            }

            @NoArgsConstructor
            @Data
            public static class TemperatureSummaryDTO {
                @JsonProperty("past6Hours")
                private Past6HoursDTO past6Hours;
                @JsonProperty("past12Hours")
                private Past12HoursDTO past12Hours;
                @JsonProperty("past24Hours")
                private Past24HoursDTO past24Hours;

                @NoArgsConstructor
                @Data
                public static class Past6HoursDTO {
                    @JsonProperty("minimum")
                    private MinimumDTO minimum;
                    @JsonProperty("maximum")
                    private MaximumDTO maximum;

                    @NoArgsConstructor
                    @Data
                    public static class MinimumDTO {
                        @JsonProperty("value")
                        private Double value;
                        @JsonProperty("unit")
                        private String unit;
                        @JsonProperty("unitType")
                        private Integer unitType;
                    }

                    @NoArgsConstructor
                    @Data
                    public static class MaximumDTO {
                        @JsonProperty("value")
                        private Double value;
                        @JsonProperty("unit")
                        private String unit;
                        @JsonProperty("unitType")
                        private Integer unitType;
                    }
                }

                @NoArgsConstructor
                @Data
                public static class Past12HoursDTO {
                    @JsonProperty("minimum")
                    private MinimumDTO minimum;
                    @JsonProperty("maximum")
                    private MaximumDTO maximum;

                    @NoArgsConstructor
                    @Data
                    public static class MinimumDTO {
                        @JsonProperty("value")
                        private Double value;
                        @JsonProperty("unit")
                        private String unit;
                        @JsonProperty("unitType")
                        private Integer unitType;
                    }

                    @NoArgsConstructor
                    @Data
                    public static class MaximumDTO {
                        @JsonProperty("value")
                        private Double value;
                        @JsonProperty("unit")
                        private String unit;
                        @JsonProperty("unitType")
                        private Integer unitType;
                    }
                }

                @NoArgsConstructor
                @Data
                public static class Past24HoursDTO {
                    @JsonProperty("minimum")
                    private MinimumDTO minimum;
                    @JsonProperty("maximum")
                    private MaximumDTO maximum;

                    @NoArgsConstructor
                    @Data
                    public static class MinimumDTO {
                        @JsonProperty("value")
                        private Double value;
                        @JsonProperty("unit")
                        private String unit;
                        @JsonProperty("unitType")
                        private Integer unitType;
                    }

                    @NoArgsConstructor
                    @Data
                    public static class MaximumDTO {
                        @JsonProperty("value")
                        private Double value;
                        @JsonProperty("unit")
                        private String unit;
                        @JsonProperty("unitType")
                        private Integer unitType;
                    }
                }
            }
        }
    }
}
